package com.sc.study.exam.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.sc.common.config.SCConfig;
import com.sc.common.utils.file.FileUploadUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sc.common.annotation.Log;
import com.sc.common.core.controller.BaseController;
import com.sc.common.core.domain.AjaxResult;
import com.sc.common.enums.BusinessType;
import com.sc.study.exam.domain.ScTestexam;
import com.sc.study.exam.service.IScTestexamService;
import com.sc.common.utils.poi.ExcelUtil;
import com.sc.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 考试Controller
 *
 * @author sc
 * @date 2022-02-27
 */
@RestController
@RequestMapping("/study/testexam")
public class ScTestexamController extends BaseController {
    @Autowired
    private IScTestexamService scTestexamService;

    @Autowired
    private SCConfig scConfig;

    /**
     * 查询考试列表
     */
    @PreAuthorize("@ss.hasPermi('study:testexam:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScTestexam scTestexam) {
        startPage();
        List<ScTestexam> list = scTestexamService.selectScTestexamList(scTestexam);
        return getDataTable(list);
    }

    /**
     * 导出考试列表
     */
    @PreAuthorize("@ss.hasPermi('study:testexam:export')")
    @Log(title = "考试", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScTestexam scTestexam) {
        List<ScTestexam> list = scTestexamService.selectScTestexamList(scTestexam);
        ExcelUtil<ScTestexam> util = new ExcelUtil<ScTestexam>(ScTestexam.class);
        util.exportExcel(response, list, "考试数据");
    }

    /**
     * 获取考试详细信息
     */
    @PreAuthorize("@ss.hasPermi('study:testexam:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(scTestexamService.selectScTestexamById(id));
    }

    /**
     * 新增考试， 批量新增学生试卷
     */
    @PreAuthorize("@ss.hasPermi('study:testexam:add')")
    @Log(title = "考试", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(ScTestexam scTestexam, MultipartFile[] multipartFiles) throws IOException {
        StringBuffer sb = new StringBuffer();
        // 上传文件路径
        String filePath = SCConfig.getUploadPath();
        if (multipartFiles != null && multipartFiles.length > 0) {
            for (MultipartFile m : multipartFiles) {
                String fileName = FileUploadUtils.upload(filePath, m);
                sb.append(m.getOriginalFilename()+"|.|"+fileName+"|..|");
            }
        }
        scTestexam.setCreateTime(new Date());
        scTestexam.setAttachment(sb.toString());
        scTestexam.setUserid(getLoginUser().getUserId());

        return toAjax(scTestexamService.insertScTestExamAndStudentTestExam(scTestexam));
    }


    /**
     * 新增考试
     */
    @PreAuthorize("@ss.hasPermi('study:testexam:add')")
    @Log(title = "考试", businessType = BusinessType.INSERT)
    @PostMapping("/addTestExam")
    public AjaxResult addTestExam(ScTestexam scTestexam, MultipartFile[] multipartFiles) throws IOException {
        StringBuffer sb = new StringBuffer();
        // 上传文件路径
        String filePath = SCConfig.getUploadPath();
        if (multipartFiles != null && multipartFiles.length > 0) {
            for (MultipartFile m : multipartFiles) {
                String fileName = FileUploadUtils.upload(filePath, m);
                sb.append(m.getOriginalFilename()+"|.|"+fileName+"|..|");
            }
        }
        scTestexam.setCreateTime(new Date());
        scTestexam.setAttachment(sb.toString());
        scTestexam.setUserid(getLoginUser().getUserId());
        return toAjax(scTestexamService.addScTestexam(scTestexam));
    }


    /**
     * 修改考试
     */
    @PreAuthorize("@ss.hasPermi('study:testexam:edit')")
    @Log(title = "考试", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScTestexam scTestexam) {
        return toAjax(scTestexamService.updateScTestexam(scTestexam));
    }

    /**
     * 删除考试
     */
    @PreAuthorize("@ss.hasPermi('study:testexam:remove')")
    @Log(title = "考试", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {

        return toAjax(scTestexamService.deleteScTestexamByIds(ids));
    }
}
